package com.techchat.backend.mapper;

import com.techchat.backend.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Insert("INSERT INTO users(username, password, nickname, created_at, avatar) " +
            "VALUES(#{username}, #{password}, #{nickname}, #{createdAt}, #{avatar})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);

    @Select("SELECT id, username, password, nickname, created_at AS createdAt, avatar " +
            "FROM users WHERE username = #{username}")
    User findByUsername(String username);

    // -------- 查询所有用户昵称 -----------
    @Select("SELECT nickname FROM users")
    List<String> findAllNicknames();

    // -------- 查询所有用户完整信息 -----------
    @Select("SELECT id, username, nickname, avatar FROM users")
    List<User> findAllUsers();

    @Update("UPDATE users SET nickname = #{newNickname} WHERE username = #{username}")
    void updateNickname(@Param("username") String username, @Param("newNickname") String newNickname);

    @Update("UPDATE users SET avatar = #{avatarUrl} WHERE username = #{username}")
    void updateAvatar(@Param("username") String username, @Param("avatarUrl") String avatarUrl);
}
